import {deepCopy} from '../utils/utils';
import _ from 'lodash';
import { routerRedux } from 'dva/router';
import {addTestReport,getReportList,editBasicReport,editTextReport,queryActiveReport,deleteTestReport,setTestData,releaseReport,queryBasicData,deleteBasicData,addBasicData} from '../services/testreport'

export default {
  namespace: 'testreport',

  state: {
    list:[],
    basicvisible: false,
    editvisible1:false,
    editvisible2:false,
    editvisible3:false,
    activeReport:'',
    pc:[],
    web:[],
    mobile:[]
  },

  effects: {
    *showBasicFrom({ payload }, { call, put }){
      yield put({
        type: 'setFormModalVisible',
        payload: true
      })
    },

    *add({payload},{call,put}){
      let reponse = yield call(addTestReport,payload);  
    },

    *getReportList({payload},{call,put}){
      let reponse = yield call(getReportList,payload);
      if(reponse.code === 0){
        yield put({
          type:'saveReportList',
          payload: reponse.data.reportList
        })
      }
    },
    *gotoReportPage({payload},{call,put}){
      let result = yield call(queryActiveReport, payload.key)
      if(result.code == 0){
        yield put({
          type:'saveActiveReport',
          payload:result.data
        })
      }
      yield put(routerRedux.push(`/testreport/report/${payload.key}`));
    },
    *editBasicReport({payload},{call,put}){
      let reponse = yield call(editBasicReport,payload)
      if(reponse.code === 0){
        let result = yield call(queryActiveReport, payload.id)
        if(result.code == 0){
          yield put({
            type:'saveActiveReport',
            payload:result.data
          })
        }
      }
    },
    *editTextReport({payload},{call,put}){
      let reponse = yield call(editTextReport,payload)
      if(reponse.code === 0){
        let result = yield call(queryActiveReport, payload.id)
        if(result.code == 0){
          yield put({
            type:'saveActiveReport',
            payload:result.data
          })
        }
      }
    },

    *queryActiveReport({payload},{call,put}){
      let id = parseInt(payload)
      let result = yield call(queryActiveReport, id)
     // console.log('data...',result.data)
      if(result.code == 0){
        yield put({
          type:'saveActiveReport',
          payload:result.data
        })
      }
    },

    *deleteReport({payload},{call}){
      let reponse = yield call(deleteTestReport,payload)
    },

    *updetaTestData({payload},{call,put}){
      let reponse = yield call(setTestData,payload)
      if(reponse.code==0){
        let result = yield call(queryActiveReport,payload.id)
        if(result.code==0){
          yield put({
            type:'saveActiveReport',
            payload:result.data
          })
        }
      }
    },

    *releaseReport({payload},{call,put}){
      let reponse = yield call(releaseReport,payload)
      if(reponse.code == 0){
        let result = yield call(queryActiveReport,payload.id)
        if(result.code == 0){
          yield put({
            type:'saveActiveReport',
            payload:result.data
          })
        }
      }
    },

    *getDataArray({payload},{call,put}){
      let res = yield call(queryBasicData)
      if(res.code ==0){
        let data = res.data
        console.log('qwe..',data)
        yield put({
          type:'saveBasicData',
          payload:data
        })
      }   
    },

    *addNewBasicData({payload},{call,put}){
      let res = yield call(addBasicData,payload)
      if(res.code ==0){
        let res2 = yield call(queryBasicData)
        if(res2.code==0){
          yield put({
            type:'saveBasicData',
            payload:res2.data
          })
        }
      }
    },

    *deleteBasicData({payload},{call,put}){
      let res = yield call(deleteBasicData,payload)
      if(res.code ==0){
        let res2 = yield call(queryBasicData)
        if(res2.code==0){
          yield put({
            type:'saveBasicData',
            payload:res2.data
          })
        }
      }
    }
  },

  reducers: {
    setFormModalVisible(state, action) {
      return {
        ...state,
        basicvisible: action.payload
      }
    },

    setEditModalvisible(state,action) {
      let data = Object.assign({},state,action.payload)
      return{
        ...data
      }
    },

    saveReportList(state,action){
      return{
        ...state,
        list:action.payload
      }
    },

    saveActiveReport(state,action){
      return{
        ...state,
        activeReport:action.payload
      }
    },

    saveBasicData(state,action){
      console.log('action..',action.payload.pc)
      return{
        ...state,
        pc:action.payload.pc,
        web:action.payload.web,
        mobile:action.payload.mobile
      }
    },
  }
};